The problem can be found at the following link: Question Link
To solve this problem, I use a map
to store the frequency of each element. I then iterate through the map and count the elements that occur more than n/k
times.
- Time Complexity : O(n), where n is the size of the array.
- Auxiliary Space Complexity : O(n), for the map.
class Solution
{
public:
int countOccurence(int arr[], int n, int k) {
map<int,int> mp;
int minCnt = n/k;
for(int i = 0 ; i < n; ++i)
++mp[arr[i]];
int out = 0;
for(auto i: mp)
if(i.second > minCnt)
++out;
return out;
}
};
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.